<?php

/************************************

EasyCB, Community Forum Software
Copyright (C) 2007  Jonathon D. Keogh <jonathon.keogh@gmail.com>

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

$Id: page.admin.boards.php 137 2007-09-07 00:53:27Z jonathon.keogh $

************************************/

require "inc.func.php";

if(!user_loggedin() || user_level(user_name(sess_get("UserID"))) < 2)
{
	page_redirect("page.boards.php");
}

page_header("Boards");

page_section();
print "<a href=\"page.admin.php\">Return to Administration tasks</a>";
if(isset($_GET['parent']) && board_parent($_GET['parent']) != 0)
{
	print " - <a href=\"?parent=" . urlencode(board_parent($_GET['parent'])) . "\">Back to " . htmlentities(board_label(board_parent($_GET['parent']))) . "</a>";
} elseif(isset($_GET['parent']) && board_parent($_GET['parent']) == 0) {
	print " - <a href=\"?\">Back to categories</a>";
}
page_section();

if(isset($_GET['action']))
{
	$action = $_GET['action'];
	if($action == "newcat")
	{
		if(trim($_POST['category']) != "")
		{
			db_query("INSERT INTO `" . config_get("db_prefix") . "boards` (`Label`) VALUES ('" . db_escape(trim($_POST['category'])) . "');");
			$success = "Category created successfully";
		} else {
			$error = "No category name entered";
		}
	} elseif($action == "newboard") {
		if(trim($_POST['board']) != "")
		{
			db_query("INSERT INTO `" . config_get("db_prefix") . "boards` (`Label`, `Description`, `Parent`) VALUES ('" . db_escape(trim($_POST['board'])) . "', '" . db_escape(trim($_POST['description'])) . "', '" . db_escape($_GET['parent']) . "');");
			$success = "Board created successfully";
		} else {
			$error = "No board name entered";
		}
	}
}

if(isset($success))
{
	page_section("Success");
	print $success;
	page_section();
}
if(isset($error))
{
	page_section("Error");
	print "<b style=\"color: #FF0000;\">$error</b>";
	page_section();
}

if(!isset($_GET['parent']))
{
	print "<form action=\"?action=newcat\" method=\"POST\">\n";
	page_section("Create a category");
?>
Create a new category with the name:<br>
	<input name="category" size="40"> <button type="submit">Create</button>
<?php
	page_section();
	print "</form>\n";

	page_section("Board list");
	print "Select a category to proceed to create/delete boards:\n";
	print "<ul>\n";
	$cats = db_query("SELECT `ID`, `Label` FROM `" . config_get("db_prefix") . "boards` WHERE `Parent`='0';");
	if(db_num_rows($cats) == 0)
	{
		print "<li><i>There aren't any categories! Create one to get started.</i></li>\n";
	}
	while($row = db_fetch_assoc($cats))
	{
		print "<li><a href=\"?parent=" . urlencode($row['ID']) . "\">" . htmlentities($row['Label']) . "</a></li>\n";
	}
	print "</ul>\n";
	page_section();
} else {
	$parent = $_GET['parent'];
	if(!board_exists($parent))
	{
		page_redirect("page.admin.boards.php");
	}
	
	print "<form action=\"?action=newboard&amp;parent=" . urlencode($parent) . "\" method=\"POST\">\n";
	page_section("Create a board here");
?>
Create a new board with the name:<br>
	<input name="board" size="40"><br>
And the optional description:<br>
	<input name="description" size="40"> <button type="submit">Create</button>
<?php
	page_section();
	print "</form>\n";
	
	if(isset($_GET['order']))
	{
		$order = $_GET['order'];
		
		$_boards = db_query("SELECT `ID`, `Order` FROM `" . config_get("db_prefix") . "boards` WHERE `Parent`='" . db_escape($parent) . "' ORDER BY `Order` ASC;");
		$boards = Array();
		$item = -1;
		$i = 0;
		while($row = db_fetch_assoc($_boards))
		{
			$boards[$i] = $row['ID'];
			if($row['ID'] == $_GET['id'])
			{
				$item = $i;
			}
			$i++;
		}
		
		if($order == "up")
		{
			$temp = $boards[$item-1];
			$boards[$item-1] = $boards[$item];
			$boards[$item] = $temp;
		} elseif($order == "down") {
			$temp = $boards[$item+1];
			$boards[$item+1] = $boards[$item];
			$boards[$item] = $temp;
		}
		
		foreach($boards as $order => $bid)
		{
			db_query("UPDATE `" . config_get("db_prefix") . "boards` SET `Order`='" . db_escape($order) . "' WHERE `ID`='" . db_escape($bid) . "';");
		}
		
		page_section("Re-ordered");
		print "The board has been re-ordered.";
		page_section();
	}
	
	page_section("Boards within " . htmlentities(board_label($parent)));
	$boards = db_query("SELECT `ID`, `Label`, `Description` FROM `" . config_get("db_prefix") . "boards` WHERE `Parent`='" . db_escape($parent) . "' ORDER BY `Order` ASC;");
	print "<ul>\n";
	if(db_num_rows($boards) == 0)
	{
		print "<li><i>There aren't any boards within this board!</i></li>\n";
	}
	$max = db_num_rows($boards);
	$i = 0;
	while($row = db_fetch_assoc($boards))
	{
		$i++;
		
		$moveup = "";
		if($i != 1)
		{
			$moveup = "<a href=\"?parent=" . urlencode($parent) . "&amp;id=" . urlencode($row['ID']) . "&amp;order=up\">move up</a>";
		}
		
		$movedown = "";
		if($i != $max)
		{
			$movedown = "<a href=\"?parent=" . urlencode($parent) . "&amp;id=" . urlencode($row['ID']) . "&amp;order=down\">move down</a>";
		}
		
		$middle = "";
		if($movedown != "" && $moveup != "")
		{
			$middle = " | ";
		}
		
		$sides = true;
		if($movedown == "" && $moveup == "")
		{
			$sides = false;
		}
		
		print "<li><a href=\"?parent=" . urlencode($row['ID']) . "\">" . htmlentities($row['Label']) . "</a> " . ($sides ? "[" : "") . "$moveup$middle$movedown" . ($sides ? "]" : "") . "<br>" . htmlentities($row['Description']) . "</li>\n";
	}
	print "</ul>\n";
	page_section();
}

page_footer('$Id: page.admin.boards.php 137 2007-09-07 00:53:27Z jonathon.keogh $');

?>